package edu.xaut.bookingsys.controller;

import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;
import edu.xaut.bookingsys.entity.EnumCollege;
import edu.xaut.bookingsys.entity.EnumLevel;
import edu.xaut.bookingsys.entity.InfoTeacher;
import edu.xaut.bookingsys.service.InfoTeacherService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teachers")
public class TeachersController {

    @Autowired
    private InfoTeacherService infoTeacherService;

    @GetMapping("/getAllTeachers")
    public SaResult getAllTeachers() {
        List<InfoTeacher> teachersList = infoTeacherService.getAllTeachers();
        System.out.println(teachersList);

        return SaResult.data(teachersList);
    }

    // FIXME: PostMapping?可能不适合，此处参数不应以InfoTeacher做为参数，传入两个参数是不是可能更好？

    /**
     * @param teacherName
     * @param level
     * @return
     */
    // teacherName为空，levelName为空，
    @GetMapping("/search")
    public SaResult getSearchTeacher(@RequestParam String teacherName, @RequestParam Integer level) {
        System.out.println(teacherName + "@@" + level);
        assert(level < EnumLevel.values().length);
        EnumLevel l = EnumLevel.values()[level];
        if(level == 0)
            l = null;

        if (SaFoxUtil.isEmpty(teacherName.trim()) && SaFoxUtil.isEmpty(l)) {
            return SaResult.data(infoTeacherService.getAllTeachers());
        }
        return SaResult.data(infoTeacherService.searchByNameOrLevel(teacherName.trim(), l));
    }
    @GetMapping("/getLevels")
    public SaResult getLevels()
    {
        return SaResult.data(EnumLevel.values());
    }

    @GetMapping("/getColleges")
    public SaResult getColleges()
    {
        return SaResult.data(EnumCollege.values());
    }

    @PostMapping("/insertTeacher")
    public SaResult insertTeacher(@RequestBody InfoTeacher infoTeacher){
        //初始化密码为123456
        infoTeacher.setPassword("123456");
        return SaResult.data(infoTeacherService.insertTeacher(infoTeacher));
    }

    @PostMapping("/deleteTeacher/{id}")
    public SaResult deleteTeacher(@PathVariable int id){
        infoTeacherService.deleteTeacher(id);
        return SaResult.ok();
    }

    @PostMapping("/updateTeacher")
    public SaResult updateTeacher(@RequestBody InfoTeacher infoTeacher){
        infoTeacherService.updateTeacher(infoTeacher);
        return SaResult.ok();
    }
}
